package com.hbandroid.myscrolldemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.hbandroid.myscrolldemo.util.CommonUtil;

/**
 * Title:MyScrollDemo
 * <p>
 * Description:
 * <p>
 * <p>
 * Author: baigege(baigegechen@gmail.com)
 * <p>
 * Date：2017-08-10
 */

public class MyAnimationView extends View {

    private int mWidth;
    private int mHeight;

    private int mDrawWidth;
    private int mDrawHeight;

    private boolean mWidthMode = true;

    private Paint mArcPaint;
    private Paint mTextPaint;

    private boolean isOnce = true;

    private static final int DEFALUT_SIZE = 12;

    private String defalut_text = "当前速度：100km/h";

    private static final int DEFALUT_LR_PADDING = 10;
    private static final int DEFALUT_TB_PADDING = 4;


    public MyAnimationView(Context context) {
        this(context, null);
    }

    public MyAnimationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mArcPaint = new Paint();
        mArcPaint.setColor(Color.BLUE);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(5);
        mArcPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.RED);
        mTextPaint.setTextSize(DEFALUT_SIZE * CommonUtil.getDensity(getContext()));
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            mWidth = 200;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = widthSize;
        } else {
            mHeight = 200;
        }

        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        initDrawArea();

        if (isOnce) {
            canvas.drawArc(mWidthMode ? (mWidth - mDrawWidth) / 2 : 0, mWidthMode ? 0 : (mHeight - mDrawHeight) / 2, mWidthMode ? (mWidth - mDrawWidth) / 2 + mDrawWidth : mWidth,
                    mWidthMode ? mHeight : (mHeight - mDrawHeight) / 2 + mDrawHeight, -210, 240, false, mArcPaint);

            canvas.rotate(-210);
            for (int i = 0; i < 41; i++) {
                if (i % 5 == 0) {
                    canvas.drawLine(mWidth / 2, mWidthMode ? 0 : (mHeight - mDrawHeight) / 2, mWidth / 2, mWidthMode ? 0 : (mHeight - mDrawHeight) / 2 + 30, mArcPaint);
                } else {
                    canvas.drawLine(mWidth / 2, mWidthMode ? 0 : (mHeight - mDrawHeight) / 2, mWidth / 2, mWidthMode ? 0 : (mHeight - mDrawHeight) / 2 + 5, mArcPaint);
                }
                canvas.rotate(6);
                canvas.save();
            }
        }

//        drawCenterText(canvas);
        canvas.restore();
    }

    private void drawCenterText(Canvas canvas) {
        Rect rect = new Rect();
        mTextPaint.getTextBounds(defalut_text, 0, defalut_text.length(), rect);
        Paint.FontMetrics metrics = mTextPaint.getFontMetrics();

        canvas.drawRect(mWidthMode ? (mWidth - mDrawWidth) / 2 + (mDrawWidth - rect.right + rect.left) / 2 - DEFALUT_LR_PADDING : (mDrawWidth - rect.right + rect.left) / 2 - DEFALUT_LR_PADDING,
                mWidthMode ? mDrawHeight / 2 : (mHeight - mDrawHeight) / 2 + mDrawHeight / 2,
                mWidthMode ? (mWidth - mDrawWidth) / 2 + (mDrawWidth - rect.right + rect.left) / 2 + rect.right - rect.left + DEFALUT_LR_PADDING : (mDrawWidth - rect.right + rect.left) / 2 + rect.right - rect.left + DEFALUT_LR_PADDING,
                mWidthMode ? mDrawHeight / 2 + rect.bottom - rect.top + 2 * DEFALUT_TB_PADDING : (mHeight - mDrawHeight) / 2 + mDrawHeight / 2 + rect.bottom - rect.top + 2 * DEFALUT_TB_PADDING, mArcPaint);

        canvas.drawText(defalut_text, mWidthMode ? (mWidth - mDrawWidth) / 2 + (mDrawWidth - rect.right + rect.left) / 2 : (mDrawWidth - rect.right + rect.left) / 2,
                mWidthMode ? mDrawHeight / 2 - metrics.top : (mHeight - mDrawHeight) / 2 + mDrawHeight / 2 - metrics.top + DEFALUT_TB_PADDING, mTextPaint);
    }

    private void initDrawArea() {
        if (mHeight > mWidth) {
            mDrawHeight = mWidth;
            mDrawWidth = mDrawWidth;
            mWidthMode = false;
        } else {
            mDrawHeight = mHeight;
            mDrawWidth = mHeight;
            mWidthMode = true;
        }
    }
}
